//
//  AdoptedTrusteeView.m
//  PigMarket
//
//  Created by mac on 2019/5/14.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AdoptedTrusteeView.h"
#import "AdoptedTrusteeCell.h"

@interface AdoptedTrusteeView ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**
 collectionView
 */
@property (nonatomic , strong) UICollectionView *collectionView;

@end
@implementation AdoptedTrusteeView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initAction];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.collectionView];
}
- (void)initAction {
    
}
- (void)initLayout {
    // 布局子控件
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
}
#pragma mark - delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AdoptedTrusteeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AdoptedTrusteeCell" forIndexPath:indexPath];
    if (self.dataArr.count > 0 && self.dataArr.count > indexPath.item) {
        [cell configCellWithModel:self.dataArr[indexPath.item]];
    }
    
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AdoptedTrustee *model = self.dataArr[indexPath.row];
    NSUInteger index = indexPath.item;
    if ([model.status intValue] == 0) {//可选
        if (!model.isSelect) {//当前点击cell为未点击状态
            
            for (NSUInteger i = index; i > 0; i --) {
                AdoptedTrustee *model = self.dataArr[i];
                if ([model.status intValue] == 0) {
                    model.isSelect = YES;
                } else {
                    break;
                }
            }
        } else {//已点击状态，点击取已点击状态
            NSUInteger max = self.dataArr.count - 1;
            for (NSUInteger i = max; i > 0; i--) {
                AdoptedTrustee *model = self.dataArr[i];
                
                if (i > indexPath.row) {
                    if ([model.status intValue] == 0) {//可选
                        model.isSelect = NO;
                        
                    } else {//不可选
                        
                    }
                }
            }
        }
        //清空总金额
        float trusteeCount = 0;
        
        for (int i = 0; i < self.dataArr.count; i++) {
            AdoptedTrustee *moneyModel = self.dataArr[i];
            
            if (moneyModel.isSelect && [model.status intValue] == 0) {

                if ([moneyModel.type intValue] == 1) {//阶段
                    float  price = [moneyModel.money floatValue];
                    trusteeCount += price;
                } else if ([moneyModel.type intValue]== 2) {//总包
                    trusteeCount = [moneyModel.money floatValue];
                }
            }
        }

        NSString *priceStr = [NSString stringWithFormat:@"%.2f",trusteeCount];
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationTrusteePriceChange object:self userInfo:@{@"selectTrusteePrice":priceStr}];
        [self.collectionView reloadData];
    } else {}//不可点击
}

#pragma mark - getter and setter
- (void)setDataArr:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    [self.collectionView reloadData];
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemW = (SCREEN_WIDTH - 100) / 3;
        CGFloat itemH = 120;
        layout.itemSize = CGSizeMake(itemW, itemH);
        layout.sectionInset = UIEdgeInsetsMake(15, 20, 0, 20);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[AdoptedTrusteeCell class] forCellWithReuseIdentifier:@"AdoptedTrusteeCell"];
    }
    return _collectionView;
}

@end

